package com.sevattal.service.leaves.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sevattal.common.base.result.ResultCodeEnum;
import com.sevattal.service.base.exception.SevattalException;
import com.sevattal.service.leaves.entity.ApplyForChildOrder;
import com.sevattal.service.leaves.entity.ApplyForMainOrder;
import com.sevattal.service.leaves.entity.vo.ApplyForChildOrderVo;
import com.sevattal.service.leaves.entity.vo.ApplyForMainOrderVo;
import com.sevattal.service.leaves.entity.vo.ApplyForOrderVo;
import com.sevattal.service.leaves.entity.vo.AuditOrderVo;
import com.sevattal.service.leaves.mapper.ApplyForChildOrderMapper;
import com.sevattal.service.leaves.mapper.ApplyForMainOrderMapper;
import com.sevattal.service.leaves.service.ApplyForOrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 申领主表 服务实现类
 * </p>
 *
 * @author Sevattal
 * @since 2021-08-28
 */
@Service
public class ApplyForOrderServiceImpl  implements ApplyForOrderService {

    @Autowired
    private ApplyForMainOrderMapper  mainOrderMapper;

    @Autowired
    private ApplyForChildOrderMapper childOrderMapper;


    @Override
    public boolean createApplyOrder(ApplyForOrderVo applyForOrderVo) {
        if (applyForOrderVo.getFAmid() == null || StringUtils.isEmpty(applyForOrderVo.getFAmid()) ||
        applyForOrderVo.getApplyForChildOrderVoList().size() == 0){
            throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }
        // 组装ApplyForMainOrder
        QueryWrapper<ApplyForMainOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("f_amId",applyForOrderVo.getFAmid());
        int count = mainOrderMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }
        // 执行数据库插入操作
        mainOrderMapper.insert(getApplyForMainOrder(applyForOrderVo));
        // 组装ApplyForChildOrder
        List<ApplyForChildOrder> applyForChildOrderList = getApplyForChildOrder(applyForOrderVo);
        for (int i=0;i<applyForChildOrderList.size();i++){
            childOrderMapper.insert(applyForChildOrderList.get(i));
        }
        return true;
    }

    @Override
    public boolean updateApplyOrder(ApplyForOrderVo applyForOrderVo) {
        // 初步字符串检查
        if (applyForOrderVo.getFAmid() == null || StringUtils.isEmpty(applyForOrderVo.getFAmid())){
            throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }
        // 主表部分更新
        QueryWrapper<ApplyForMainOrder> mainQueryWrapper = new QueryWrapper<>();
        mainQueryWrapper.eq("f_amId",applyForOrderVo.getFAmid());
        int count = mainOrderMapper.selectCount(mainQueryWrapper);
        if (count == 0) {
            throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }
        // 执行数据库更新操作
        mainOrderMapper.update( getApplyForMainOrder(applyForOrderVo),mainQueryWrapper);

        // 子表部分更新操作，为了节省时间，使用先删除后插入的方式
        List<ApplyForChildOrder> applyForChildOrderList = getApplyForChildOrder(applyForOrderVo);
        QueryWrapper<ApplyForChildOrder> childQueryWrapper = new QueryWrapper<>();
        childQueryWrapper.eq("f_amId",applyForOrderVo.getFAmid());
        childOrderMapper.delete(childQueryWrapper);
        for (int i = 0;i<applyForChildOrderList.size();i++) {
            childOrderMapper.insert(applyForChildOrderList.get(i));
        }
        return true;
    }

    @Override
    public boolean deleteApplyOrder(String id) {
        QueryWrapper<ApplyForMainOrder> mainOrderQueryWrapper = new QueryWrapper<>();
        mainOrderQueryWrapper.eq("f_amId",id);
        mainOrderMapper.delete(mainOrderQueryWrapper);
        QueryWrapper<ApplyForChildOrder> childOrderQueryWrapper = new QueryWrapper<>();
        childOrderQueryWrapper.eq("f_amId",id);
        childOrderMapper.delete(childOrderQueryWrapper);
        return true;
    }

    @Override
    public List<ApplyForOrderVo> getApplyOrderList(ApplyForOrderVo applyForOrderVo) {
        // 获取主表信息
        QueryWrapper<ApplyForMainOrder> mainOrderQueryWrapper = new QueryWrapper<>();
        mainOrderQueryWrapper.orderByAsc("am.f_amId");
        mainOrderQueryWrapper.eq("am.is_deleted",0);
        mainOrderQueryWrapper.eq("am.is_disabled",0);
        if (applyForOrderVo.getFAmid() != null  && !StringUtils.isEmpty(applyForOrderVo.getFAmid())){
            mainOrderQueryWrapper.like("am.f_amId",applyForOrderVo.getFAmid());
        }
        if (applyForOrderVo.getFEname() != null && !StringUtils.isEmpty(applyForOrderVo.getFEname())){
            mainOrderQueryWrapper.like("e1.f_eName",applyForOrderVo.getFEname());
        }
        if (applyForOrderVo.getFAuditname() != null && !StringUtils.isEmpty(applyForOrderVo.getFAuditname())){
            mainOrderQueryWrapper.like("e2.f_auditName", applyForOrderVo.getFAuditname());
        }
        if (applyForOrderVo.getFAmstatus() != null && !StringUtils.isEmpty(applyForOrderVo.getFAmstatus())){
            mainOrderQueryWrapper.eq("am.f_amStatus",applyForOrderVo.getFAmstatus());
        }
        // ge 大于等于
        if (applyForOrderVo.getSearchStartDate()!= null){
            mainOrderQueryWrapper.ge("am.f_amDate",applyForOrderVo.getSearchStartDate());
        }
        // le 小于等于
        if (applyForOrderVo.getSearchEndDate() != null) {
            mainOrderQueryWrapper.le("am.f_amDate",applyForOrderVo.getSearchEndDate());
        }
        List<ApplyForMainOrderVo> mainOrderList= mainOrderMapper.getMainOrderList(mainOrderQueryWrapper);

        // 用于最后返回给前端的Vo对象
        List<ApplyForOrderVo> applyForOrderVoList = new ArrayList<>();
        // 获取子表信息
        QueryWrapper<ApplyForChildOrder> childOrderQueryWrapper = new QueryWrapper<>();
        for (int i=0;i<mainOrderList.size();i++){
            childOrderQueryWrapper.eq("ac.f_amId",mainOrderList.get(i).getFAmid());
            childOrderQueryWrapper.eq("ac.is_deleted",0);
            childOrderQueryWrapper.eq("ac.is_disabled",0);
            List<ApplyForChildOrderVo> childOrderList = childOrderMapper.getChildOrderList(childOrderQueryWrapper);
            // 将主表信息传入返回对象
            applyForOrderVoList.add(getApplyForOrderVo(mainOrderList.get(i),childOrderList));
            //清空 querywrapper 查询条件
            childOrderQueryWrapper.clear();
        }
        return applyForOrderVoList;
    }

    @Override
    public boolean auditOrder(AuditOrderVo auditOrderVo) {
        //数据检查
        if (auditOrderVo.getAuditId() == null || StringUtils.isEmpty(auditOrderVo.getAuditId())||
        auditOrderVo.getOrderId() == null || StringUtils.isEmpty(auditOrderVo.getOrderId()) ||
        auditOrderVo.getAuditStatus() == null || StringUtils.isEmpty(auditOrderVo.getAuditStatus())){
            throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }

        QueryWrapper<ApplyForMainOrder> mainQueryWrapper = new QueryWrapper<>();
        mainQueryWrapper.eq("f_amId",auditOrderVo.getOrderId());
        // 组装主表
        ApplyForMainOrder applyForMainOrder = new ApplyForMainOrder();
        applyForMainOrder.setFAuditid(auditOrderVo.getAuditId());
        applyForMainOrder.setFAmstatus(auditOrderVo.getAuditStatus());
        int result = mainOrderMapper.update(applyForMainOrder,mainQueryWrapper);
        return result == 1;
    }

    // 组装ApplyForMainOrder 的方法
    public ApplyForMainOrder getApplyForMainOrder(ApplyForOrderVo applyForOrderVo){
        // 组装ApplyForMainOrder
        ApplyForMainOrder applyForMainOrder = new ApplyForMainOrder();
        applyForMainOrder.setFAmid(applyForOrderVo.getFAmid());
        applyForMainOrder.setFEid(applyForOrderVo.getFEid());
        applyForMainOrder.setFWhid(applyForOrderVo.getFWhid());
        applyForMainOrder.setFAuditid(applyForOrderVo.getFAuditid());
        applyForMainOrder.setFAmdate(applyForOrderVo.getFAmdate());
        applyForMainOrder.setFAmstatus(applyForOrderVo.getFAmstatus());
        applyForMainOrder.setFAmconfirm(applyForOrderVo.getFAmconfirm());
        applyForMainOrder.setFAmmemo(applyForOrderVo.getFAmmemo());
        return applyForMainOrder;
    }
    // 组装ApplyForChildOrder 的方法
    public List<ApplyForChildOrder> getApplyForChildOrder(ApplyForOrderVo applyForOrderVo) {
        List<ApplyForChildOrder> applyForChildOrderList = new ArrayList<>();
        ApplyForChildOrder applyForChildOrder = new ApplyForChildOrder();
        for (int i = 0; i < applyForOrderVo.getApplyForChildOrderVoList().size(); i++) {
            applyForChildOrder.setFAmid(applyForOrderVo.getFAmid());
            applyForChildOrder.setFMid(applyForOrderVo.getApplyForChildOrderVoList().get(i).getFMid());
            applyForChildOrder.setFAcquantity(applyForOrderVo.getApplyForChildOrderVoList().get(i).getFAcquantity());
            applyForChildOrder.setFAcdate(applyForOrderVo.getFAmdate());
            applyForChildOrder.setFAcstatus(applyForOrderVo.getFAmstatus());
            applyForChildOrder.setFAcmemo(applyForOrderVo.getApplyForChildOrderVoList().get(i).getFAcmemo());
            applyForChildOrderList.add(applyForChildOrder);
        }
        return applyForChildOrderList;
    }
    // 将查询到的主表、子表信息写入到返回Vo对象
    public ApplyForOrderVo getApplyForOrderVo(ApplyForMainOrderVo mainOrderVo,List<ApplyForChildOrderVo> childOrderVoList){
        ApplyForOrderVo applyForOrderVo = new ApplyForOrderVo();
        applyForOrderVo.setFAmid(mainOrderVo.getFAmid());
        applyForOrderVo.setFEid(mainOrderVo.getFEid());
        applyForOrderVo.setFWhid(mainOrderVo.getFWhid());
        applyForOrderVo.setFWhname(mainOrderVo.getFWhname());
        applyForOrderVo.setFEname(mainOrderVo.getFEname());
        applyForOrderVo.setFAuditid(mainOrderVo.getFAuditid());
        applyForOrderVo.setFAuditname(mainOrderVo.getFAuditname());
        applyForOrderVo.setFAmdate(mainOrderVo.getFAmdate());
        applyForOrderVo.setFAmstatus(mainOrderVo.getFAmstatus());
        applyForOrderVo.setFAmconfirm(mainOrderVo.getFAmconfirm());
        applyForOrderVo.setFAmmemo(mainOrderVo.getFAmmemo());
        applyForOrderVo.setApplyForChildOrderVoList(childOrderVoList);
        return applyForOrderVo;
    }

}
